package com.qifan.energy.util;

import com.orhanobut.logger.Logger;

import org.json.JSONObject;

import java.lang.reflect.Field;
import java.util.WeakHashMap;

/* Map转换工具类 */
public class MapUtils {

    /*public static Map<String, Object> mapWithObject(Object obj) {
        if (obj == null) {
            return null;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            Field[] declaredFields = obj.getClass().getDeclaredFields();
            for (Field field : declaredFields) {
                field.setAccessible(true);
                map.put(field.getName(), field.get(obj));
            }
        } catch (Exception e) {
            Logger.d(e.getMessage());
        }
        return map;
    }*/

    /* 将对象转成map */
    public static WeakHashMap<String, Object> mapWithObject(Object obj) {
        if (obj == null) {
            return null;
        }
        WeakHashMap<String, Object> map = new WeakHashMap<>();
        try {
            Field[] declaredFields = obj.getClass().getDeclaredFields();
            for (Field field : declaredFields) {
                field.setAccessible(true);
                Object value = field.get(obj);
                if (null != value) {
                    map.put(field.getName(), value);
                }
            }
        } catch (Exception e) {
            Logger.d(e.getMessage());
        }
        return map;
    }

    /* 将对象转成JSONObject */
    public static JSONObject jsonObjectWithObject(Object obj) {
        if (obj == null) {
            return null;
        }
        JSONObject jsonObject = new JSONObject();
        try {
            Field[] declaredFields = obj.getClass().getDeclaredFields();
            for (Field field : declaredFields) {
                field.setAccessible(true);
                Object value = field.get(obj);
                if (null != value) {
                    jsonObject.put(field.getName(), value);
                }
            }
        } catch (Exception e) {
            Logger.d(e.getMessage());
        }
        return jsonObject;
    }

    /*public static ArrayMap<String, Object> mapWithObject(Object obj) {
        if (obj == null) {
            return null;
        }
        ArrayMap<String, Object> map = new ArrayMap<>();
        try {
            Field[] declaredFields = obj.getClass().getDeclaredFields();
            for (Field field : declaredFields) {
                field.setAccessible(true);
                map.put(field.getName(), field.get(obj));
            }
        } catch (Exception e) {
            Logger.d(e.getMessage());
        }
        return map;
    }*/
}
